/**
 * Copyright (c) 2009-2012 XTEAM All rights reserved.
 */
/**
 * File：ArticlesAction.java
 * History:
 *         2012-6-15: Initially created, liuzhi.
 */
package com.xteam.ecsystem.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.entities.BaseArticleGroup;
import com.xteam.ecsystem.entities.BaseArticleInfo;
import com.xteam.ecsystem.service.front.FrontArticlesService;
import com.xteam.ecsystem.system.ConfigManager;
import com.xteam.ecsystem.utils.FrontUtil;

/**
 * 站内讯息请求控制层。
 * @author liuzhi
 */
@Controller
public class ArticlesAction {
	
	@Autowired
	private ConfigManager cfgManager;
	
	@Autowired
	private FrontArticlesService articlesService;
	
	/**
	 * 站内快讯列表
	 * @author liuzhi Apr 26, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/articles.xhtml", method = RequestMethod.GET)
	public String articles(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			// 文章类型
			String baiGroup = FrontUtil.getStringParam(request, "baiGroup", "");
			// 文章标题
			String baiTitle = FrontUtil.getStringParam(request, "baiTitle", "");
			// 获得文章分类信息
			BaseArticleGroup articleGroup = this.articlesService.getArticleGroup(baiGroup);
			
			//设置数据
			model.put(Constant.VAR_PAGEINDEX.value(), FrontUtil.getIntegerParam(request, "index", 1));
			model.put(Constant.VAR_PAGESIZE.value(), this.cfgManager.getSystemConfig().getSscAlpagesize());
			model.put(Constant.VAR_BAIGROUP.value(), baiGroup);
			model.put(Constant.VAR_BAITITLE.value(), baiTitle);
			model.put(Constant.VAR_ARTICLEGROUP.value(), articleGroup);
		}
		
		return template;
	}
	
	/**
	 * 站内快讯详细
	 * @author liuzhi Apr 26, 2013
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	@RequestMapping(value = "/articleDetails.xhtml", method = RequestMethod.GET)
	public String articleDetails(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException, ServletException {
		//获取模板路径
		String template = FrontUtil.getTemplatePath(request, response, this.cfgManager);
		
		if (template != null) {
			//设置公共数据
			FrontUtil.setCommons(request, response, model, this.cfgManager.getSystemConfig(), this.cfgManager.getWebConfig());
			
			// 文章编号
			String baiId = FrontUtil.getStringParam(request, "baiId", "");
			// 文章类型
			String baiGroup = FrontUtil.getStringParam(request, "baiGroup", "");
			
			// 获得站内快讯详细
			BaseArticleInfo bainfo = this.articlesService.getArticleInfo(baiId);
			// 取得前一篇站内快讯信息
			BaseArticleInfo preBaiinfo = this.articlesService.getPreArticleInfo(baiId, baiGroup, bainfo.getBaiOrder());
			//取得后一篇站内快讯信息
			BaseArticleInfo nextBaiinfo = this.articlesService.getNextArticleInfo(baiId, baiGroup, bainfo.getBaiOrder());
			// 获得文章分类信息
			BaseArticleGroup articleGroup = this.articlesService.getArticleGroup(baiGroup);
			
			// 设置数据
			model.put(Constant.VAR_ARTICLEDETAILS.value(), bainfo);
			model.put(Constant.VAR_PREARTICLEDETAILS.value(), preBaiinfo);
			model.put(Constant.VAR_NEXTARTICLEDETAILS.value(), nextBaiinfo);
			model.put(Constant.VAR_ARTICLEGROUP.value(), articleGroup);
		}
		
		return template;
	}
	
}
